package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduVideo;
import com.jf3q.common.dto.CourseDto;
import com.jf3q.common.vo.CourseVo;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.entity.Course;
import com.jf3q.front.entity.Pl;
import com.jf3q.front.service.EduChapterService;
import com.jf3q.front.service.EduCourseService;
import com.jf3q.front.service.EduVideoService;
import com.jf3q.front.service.PlService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eduservice")
public class CourseController {
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    PlService plService;
    //redis
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/getByIdAll/{id}")
    public ResultVo getByIdAll(@PathVariable String id){
        CourseVo courseVo = eduCourseService.listAllById(id);
        return ResultVo.success("",courseVo);
    }

    //查询所有大纲
    @PostMapping("/Course/{id}")
    public ResultVo chapterAll(@PathVariable String id){
        LambdaQueryWrapper<EduChapter> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduChapter::getCourseId,id);
        List<EduChapter> list = eduChapterService.list(lambdaQueryWrapper);
        for (EduChapter chapter : list) {
            LambdaQueryWrapper<EduVideo> videoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            videoLambdaQueryWrapper.eq(EduVideo::getChapterId,chapter.getId());
            List<EduVideo> list1 = eduVideoService.list(videoLambdaQueryWrapper);
            chapter.setVideos(list1);
        }
        return ResultVo.success("",list);
    }
//    @GetMapping("/getHotCourse")
//    @Operation(summary = "查询热门课程")
//    public ResultVo HotCourse(){
//        List<EduCourse> eduCreate = eduCourseService.getHotEduCourse();
//        return ResultVo.success("查询热门课程",eduCreate);
//    }
    @GetMapping("/getHotCourse")
    @Operation(summary = "查询热门课程")
    public ResultVo HotCourse(){
        String key = "course";
        Boolean bool = redisTemplate.hasKey(key);
        if (bool){
            List<EduCourse> eduCreate = eduCourseService.getHotEduCourse();
            redisTemplate.opsForList().set(key, 0, eduCreate);
        }else {
            List<EduCourse> eduCreate = eduCourseService.getHotEduCourse();
            redisTemplate.opsForList().leftPushAll(key, eduCreate);
        }
        Object redisCourseList = redisTemplate.opsForList().index(key,0);
        return ResultVo.success("查询热门课程",redisCourseList);
    }

    @PostMapping("/getAllCourse/{pageCount}")
    @Operation(summary = "查询课程")
    public ResultVo getAllCourse(@RequestBody CourseDto courseDto, @PathVariable Integer pageCount){
        LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(courseDto)) {
            //根据课程标题查询
            if (StringUtils.isNotEmpty(courseDto.getTitle())) {
                queryWrapper.like(EduCourse::getTitle, courseDto.getTitle());
            }
            //根据状态查询
            if (StringUtils.isNotEmpty(courseDto.getStatus())) {
                queryWrapper.eq(EduCourse::getStatus, courseDto.getStatus());
            }
            //根据销量排序
            if (StringUtils.isNotEmpty(courseDto.getBuyCountSort())) {
                queryWrapper.orderByDesc(EduCourse::getBuyCount);
            }
            //根据时间排序
            if (StringUtils.isNotEmpty(courseDto.getGmtCreateSort())) {
                queryWrapper.orderByDesc(EduCourse::getEduCreate);
            }
            //根据价格排序
            if (StringUtils.isNotEmpty(courseDto.getPriceSort())) {
                queryWrapper.orderByDesc(EduCourse::getPrice);
            }
            //根据一级分类查询
            if (StringUtils.isNotEmpty(courseDto.getSubjectParentId())) {
                queryWrapper.eq(EduCourse::getSubjectParentId, courseDto.getSubjectParentId());
            }
            //根据二级分类查询
            if (StringUtils.isNotEmpty(courseDto.getSubjectId())) {
                queryWrapper.eq(EduCourse::getSubjectId, courseDto.getSubjectId());
            }
        }
        Page<EduCourse> page = eduCourseService.page(new Page<>(courseDto.getPageNow(), pageCount), queryWrapper);
        return ResultVo.success("查询所有课程",page);
    }

    //查询所有差评
    @PostMapping("/cha1/{id}")
    public ResultVo cha(@PathVariable String id) {
        List<Pl> getpl = plService.cha(id, 1);
        return ResultVo.success("", getpl);
    }
    //查询所有中评
    @PostMapping("/cha2/{id}")
    public ResultVo cha2(@PathVariable String id) {
        List<Pl> getpl = plService.cha(id, 2);
        return ResultVo.success("", getpl);
    }
    //查询所有好评
    @PostMapping("/cha3/{id}")
    public ResultVo cha3(@PathVariable String id) {
        List<Pl> getpl = plService.cha(id, 3);
        return ResultVo.success("", getpl);
    }


    //提交评论
    @PostMapping("/tijiao")
    public ResultVo tijiao(@RequestBody Pl pl) {
        try {
            plService.save(pl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("", null);
    }
}